# 
# LTParser.rb
# 
# Created on Oct 21, 2007, 10:31:38 PM
# 
# To change this template, choose Tools | Templates
# and open the template in the editor.
 
require 'active_record'
require "fastercsv"
 

  FasterCSV.foreach("C:\\Documents\\LibraryDB\\LibraryThing_export_full.csv",
                             :headers           => true,
                             :header_converters => :symbol) do |row|
                               puts row[0] << " ---- " <<row[1]

  end                               

ActiveRecord::Base.logger = Logger.new(STDERR)
ActiveRecord::Base.colorize_logging = false

ActiveRecord::Base.establish_connection(
    :adapter => "mysql",
    :host  => "localhost",
    :database => "LibraryDB"
)

ActiveRecord::Schema.define do
    create_table :Books do |table|
        table.column :title, :string
        table.column :author_id, :integer
    end

    create_table :authors do |table|
        table.column :nameFirst, :string
        table.column :nameLast, :string
    end
end

class Author < ActiveRecord::Base
    has_many :books
end

class Book < ActiveRecord::Base
    belongs_to :authors
end

author = Author.create(:nameFirst => 'Isaac', :nameLast => 'Asimov')
author.books.create(:title => 'Foundation')
author.books.create(:title => 'I Robot')
author.books.create(:title => 'Foundations Edge')

puts Author.find(1).books.length

puts Book.find_by_title('Foundation').title

